YUI.add('wfwraper', function (Y) {
	
//	var v = /[-a-zA-Z0-9@:%_\+.~#?&\/=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&\/=]*)?/gi;
//	var v = /(https?:(&#x2F;){2}){1}([-a-zA-Z0-9]{2,25}\.[-a-zA-Z0-9]{2,25}\.[-a-zA-Z0-9]{2,4}(&#x2F;)?((#|\?){1}([-a-zA-Z0-9@;:%_\+.~#?&\/=]){,256})?)$/gi;
	var urlr = /(https?:\/{2}){1}(([-a-zA-Z0-9]{1,25}\.){1,4}[-a-zA-Z0-9]{1,4}(:\d{1,6})?([-a-zA-Z0-9@:%_\+.~#?&\/=]{0,256})?)/gi;
	
	//var urlr1 = / +([-a-zA-Z0-9]{1,25}\.){1,4}[-a-zA-Z0-9]{1,4}(:\d{1,6})?([-a-zA-Z0-9@:%_\+.~#?&\/=]{0,256})? +/gi;
	
//	[url=http://wikipedia.org]Wikipedia[/url]
//	[img]http://upload.wikimedia.org/wikipedia/commons/thumb/6/63/Wikipedia-logo.png/72px-Wikipedia-logo.png[/img]
	var bburlr = /\[url=([^\]]+)\]([^\[]+)\[\/url]/gi;
	
	var assetr = /\/asset\/(\d+)(\.\w{1,4})?/gi;
	
	//[img]http://upload.wikimedia.org/wikipedia/commons/thumb/6/63/Wikipedia-logo.png/72px-Wikipedia-logo.png[/img]
	var imgurlr = /\[img\]([^\[]+)\[\/img\]/gi;
	
	var anchorRegex = /<a href="[^ ]+" target="_blank">[^<]+<\/a>/gi;
	var imgRegex = /<img src="[^ ]+">/gi;
	
	function getBeforAfter(totalStr,findedStr,lastIndex){
		var startPisition = lastIndex - findedStr.length;
		return [totalStr.substr(0,startPisition),totalStr.substr(lastIndex)];
	}
	
	function startWith(str,s){
		if(str && str.length > 0){
			return str.substr(0,1) == s;
		}else{
			return false;
		}
	}
	
	function endWith(str,s){
		if(str && str.length > 0){
			return str.substr(str.length - 1) == s;
		}else{
			return null;
		}
	}
	
	function sanitize(s,aa){
		var i = 0;
		for(;i<aa.length;i++){
		       for(var j=0;j<i;j++){
		           if(aa[i][0]<aa[j][0]){
		           		var temp = aa[i];
		           		aa[i]=aa[j];
		           		aa[j]=temp;
		           }
		       }
		   }
		var aa1 = [];
		var lastend = 0;
		i = 0;
		for(;i<aa.length;i++){
			aa1.push([lastend,aa[i][0]]);
			lastend = aa[i][1];
		}
		var ss = "";
		i = 0;
		for(;i<aa.length;i++){
			ss += Y.Escape.html(s.substring(aa1[i][0],aa1[i][1]));
			ss += s.substring(aa[i][0],aa[i][1]);
//			Y.log(ss);
		}
		if(i>0){
			ss += Y.Escape.html(s.substr(aa[i-1][1]));
			return ss;
		}else{
			return Y.Escape.html(s);
		}
		
	}

	
	
	
	Y.Wfwrapper = function(str){
		var result;
//		var s = str.replace(urlr,'<a href="$1$2" target="_blank">$2</a>');
		
		while ((result = imgurlr.exec(str)) != null)  {
//			Y.log(result);
			  var p = result[0];//匹配字串
			  var li = imgurlr.lastIndex;
			  var banda = getBeforAfter(str, p, li);
			  if(endWith(banda[0],'"') || startWith(banda[1],'"')){
				  continue;
			  }
			  var tpl = '<img src="{url}">';
			  var m = {url:result[1]};
			  var tplr = Y.Lang.sub(tpl,m);
			  imgurlr.lastIndex = li + (tplr.length - p.length);
			  str = banda[0] + tplr + banda[1];
		};
		
		while ((result = bburlr.exec(str)) != null)  {
			  var p = result[0];//匹配字串
			  var li = bburlr.lastIndex;
			  var banda = getBeforAfter(str, p, li);
			  if(endWith(banda[0],'"') || startWith(banda[1],'"')){
				  continue;
			  }
			  var tpl = '<a href="{url}" target="_blank">{urlname}</a>';
			  var m = {url:result[1],urlname:result[2]};
			  var tplr = Y.Lang.sub(tpl,m);
			  bburlr.lastIndex = li + (tplr.length - p.length);
			  str = banda[0] + tplr + banda[1];
		};

		while ((result = urlr.exec(str)) != null)  {
			  var p = result[0];//匹配字串
			  var li = urlr.lastIndex;
			  var banda = getBeforAfter(str, p, li);
			  if(endWith(banda[0],'"') || startWith(banda[1],'"')){
				  continue;
			  }
			  var tpl = '<a href="{url}" target="_blank">{urlname}</a>';
			  var m = {url:p,urlname:result[2]};
			  var tplr = Y.Lang.sub(tpl,m);
			  urlr.lastIndex = li + (tplr.length - p.length);
			  str = banda[0] + tplr + banda[1];
		};
		
		while ((result = assetr.exec(str)) != null)  {
			  var p = result[0];//匹配字串
			  var banda = getBeforAfter(str, p, assetr.lastIndex);
			  if(endWith(banda[0],'"') || startWith(banda[1],'"')){
				  continue;
			  }
			  var tpl = '<a href="{url}" target="_blank">附件</a>';
			  var tplr = Y.Lang.sub(tpl,{url:p});
			  assetr.lastIndex = assetr.lastIndex + (tplr.length - p.length);
			  str = banda[0] + tplr + banda[1];
		};
		
		var startEnds = [];
		while ((result = anchorRegex.exec(str)) != null)  {
			  var p = result[0];
			  var start = anchorRegex.lastIndex - p.length;
			  var end = anchorRegex.lastIndex;
			  startEnds.push([start,end]);
//			  Y.log('start:' + start + ',end:' + end);
		};
		
		while ((result = imgRegex.exec(str)) != null)  {
			  var p = result[0];
			  var start = imgRegex.lastIndex - p.length;
			  var end = imgRegex.lastIndex;
			  startEnds.push([start,end]);
//			  Y.log('start:' + start + ',end:' + end);
		};
//		s = s.replace(assetr,'<a href="$1" target="_blank">附件</a>');
		var sanitized = sanitize(str, startEnds);
//		Y.log(sanitized);
		return sanitized;
	}
	
},'0.0.1',{requires:['escape']});